package com.remist.web.action;

import java.util.List;

import org.apache.log4j.Logger;

import com.remist.common.Constant;
import com.remist.model.ProductType;
import com.remist.service.ProductTypeService;

public class ProductTypeAction extends BaseAction {
//	protected final Log log = LogFactory.getLog(getClass());
	protected static Logger logger =Logger.getLogger(ProductTypeAction.class);
	/**	 serialVersionUID */
	private static final long serialVersionUID = 1L;
	private ProductTypeService productTypeService;
	private ProductType productType;
	private ProductType productTypeParenBean;
	private List<ProductType> productTypes;
	private List<ProductType> listAllParents;
	public List<ProductType> getListAllParents() {
		return listAllParents;
	}

	public void setListAllParents(List<ProductType> listAllParents) {
		this.listAllParents = listAllParents;
	}

	public ProductType getProductTypeParenBean() {
		return productTypeParenBean;
	}

	public void setProductTypeParenBean(ProductType productTypeParenBean) {
		this.productTypeParenBean = productTypeParenBean;
	}

	public ProductType getProductType() {
		return productType;
	}

	public List<ProductType> getProductTypes() {
		return productTypes;
	}

	public void setProductType(ProductType productType) {
		this.productType = productType;
	}

	public void setProductTypes(List<ProductType> productTypes) {
		this.productTypes = productTypes;
	}


	public ProductTypeService getProductTypeService() {
		return productTypeService;
	}

	public void setProductTypeService(ProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}

	@Override
	public String execute() throws Exception {
		return super.execute();
	}
	
//	public String productTypeList() throws Exception {
//		return SUCCESS;
//	}
//	
	public String listProductTypeByParentId() throws Exception {
		if ( productTypeParenBean == null ){
			productTypeParenBean = new ProductType();
			productTypeParenBean.setParentId(Constant.LEVEL_ROOT);
		}
		productTypes = productTypeService.listSonProductTypeByParentKey(productTypeParenBean);
		listAllParents = productTypeService.listAllParents(productTypeParenBean);
		return SUCCESS;
	}
	
	public String addProductType() throws Exception {
		productTypeService.addProductType(productType);
		return SUCCESS;
	}
	
	public String productTypeBeforeAdd() throws Exception {
		logger.info(productTypeParenBean.getParentId());
		if ( productTypeParenBean.getParentId() != Constant.LEVEL_ROOT ){
			productTypeParenBean = productTypeService.getParentTypeByParentKey(productTypeParenBean);
		}else{
			productTypeParenBean.setId( Constant.LEVEL_ROOT );
		}
		productType = null;
		return SUCCESS;
	}
	
	public String updateProductType() throws Exception {
		productTypeService.updateProductType(productType);
		return SUCCESS;
	}
	
	public String loadProductType() throws Exception {
		productType = productTypeService.getProductTypeByKey(productType);
		return SUCCESS;
	}
	
	public String productTypeBeforeUpdate() throws Exception {
		productType = productTypeService.getProductTypeByKey(productType);
		productTypeParenBean = productTypeService.getProductTypeByKey(productTypeParenBean);
		return SUCCESS;
	}
	
	public String deleteProductType() throws Exception {
		productTypeService.deleteProductType(productType);
		return SUCCESS;
	}
	
	
}
